/**
 *   FuzzyEngine - an Simple flex framework 
 *   Copyright (C) 2010 Constantin Aldea
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fuzzy.penelope.helpers {
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;

	public class Utility {

		public static function getIndexByIdComponente(elencoComponenti : ArrayCollection, idComponente : String) : int {
			for (var i : int = 0;i < elencoComponenti.length;i++) {
				if (elencoComponenti[i].idComponente == idComponente) { 
					return i;
				}
			}
			return -1;
		}

		public static function getTimeByDate(data : String) : String {
			if (data != null) 
				return data.substr(11, 5);
			else return '';
		}

		public static function trasformaDate(data : Date) : String {
			if (data != null) {
				return data.date + "-" + data.month + "-" + data.fullYear;
			} else {
				return '';
			}
		}

		public static function trasformaDateItaToUS(data : String) : String {
			if (data != null) {
				return data.substr(6, 4) + '-' + data.substr(3, 2) + '-' + data.substr(0, 2);
			} else return '';
		}

		public static function trasformaDateItaToUSExtended(data : String) : String {
			if (data != null) {
				var dataUsa : String = data.substr(6, 4) + '-' + data.substr(3, 2) + '-' + data.substr(0, 2);
				dataUsa = dataUsa + data.substr(10, 10);
				return dataUsa;
			} else return '';
		}

		public static function trasformaDateUsToIta(data : String) : String {
			if (data != null) 
				return data.substr(8, 2) + '-' + data.substr(5, 2) + '-' + data.substr(0, 4);
			else return '';
		}

		public static function trasformaStringToDate(dataString : String) : Date {
			if (dataString != null) {
				var data : Date = new Date();
				data.setHours(0, 0, 0, 0);
				data.date = Number(dataString.toString().substring(8, 2));
				data.month = Number(dataString.toString().substring(5, 2)) - 1;
				data.fullYear = Number(dataString.toString().substring(0, 4));
				return data;
			} else {
				return new Date();
			}
		}

		public function getIndexComboBox(data : String, lista : ArrayCollection) : int {
			try {							
				for (var i : int = 0;i < lista.length;i++) {
					if (data == lista[i].data) return i; 
				}
			} catch (e : Error) {
				//trace(e.message);
				return -1;
			}
			
			return -1;
		}

		
		public static function objectHasEmptyFields(object : Object) : Boolean {
			if(!object) return false;
			if(ObjectUtil.isSimple(object)) return false;
	  		
			var isEmptyObj : Boolean = true;
			var classInfo : Object = ObjectUtil.getClassInfo(object);
			for each(var property:Object in classInfo.properties) {
				if( object[property.localName] ) {
					isEmptyObj = false;
					break;
				}
			}
			return isEmptyObj;	       
		}

		
		public static function compareObjects(objectSource : Object, objectTarget : Object) : Boolean {
			if((objectSource == null) != (objectTarget == null)) {
				return false;
			}
			
			var classInfo : Object = ObjectUtil.getClassInfo(objectSource);
			for each(var property:Object in classInfo.properties) {
				//se null non registro
				if(objectTarget.hasOwnProperty(property.localName)) {
					try {
						if(ObjectUtil.isSimple(objectSource[property.localName]) && !ObjectUtil.isSimple(objectTarget[property.localName])) {
							return false;
						}
	            		
						//simple 
						if(ObjectUtil.isSimple(objectSource[property.localName])) {
							if( objectTarget[property.localName] != objectSource[property.localName] ) {
								return false;
							}	
						//se tipo non semplice, ricorsione	
						} else {
							if( !compareObjects(objectSource[property.localName], objectTarget[property.localName]) ) {
								return false;
							} 
						}
					}catch(e : Error) {
						continue;
					}	
				} else {
					return false;
				}
			}
  			
			return true;
		}

		public static function compareArrayAndObjects(objectSource : ArrayCollection, objectTarget : Object) : Boolean {
			for (var i : int = 0;i < objectSource.length;i++) {
			
				if(Utility.compareObjects(objectSource[i], objectTarget))
							return true;
			}
			return false;			
		}

		
		/**
		 * inizia a tracciare il tempo
		 */ 
		private static var startDate : Date;
		public static var isTrackingTime : Boolean = false;

		public static function startTrackingTime() : void {
			startDate = new Date(); 
			isTrackingTime = true;
		}		

		public static function stopTrackingTime() : void {
			startDate = null; 
			isTrackingTime = false;
		}

		
		/**
		 * ritorna i (milli)secondi passati
		 */ 		
		public static function getTrackedTime(ms : Boolean = false) : Number {
			if(!isTrackingTime || !startDate) {
				return 0;
			}
			var time : Number = ( (new Date()).getTime() - startDate.getTime() ) ;
  			
			if(!ms) {
				//seconds
				return time / 1000;
			}
			return time;			
		}	
	}
}
